package com.example.auto.service;

import com.api.signature.auto.service.ApiSignService;
import com.signature.auto.common.consts.ApiSignConst;
import com.signature.auto.common.exception.ApiSignException;
import com.signature.auto.common.servlet.ApiRequest;
import com.api.signature.auto.spring.ApiSignContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @author Jerry
 * @date 2024-01-25 10:19
 */

public class PlusApiSignServiceImpl implements ApiSignService {

    @Autowired
    private MySqlService mySqlService;

    @Override
    public void checkRequest(ApiRequest request) {

        // 模拟从数据库获取动态秘钥
        List<Map<String, String>> sercretList = mySqlService.getSercretList();
        Map<String, String> stringStringMap = sercretList.get(0);
        Map<String, Object> paramMap = request.getParamMap(true);
        String appId = String.valueOf(paramMap.get("appId"));
        // TODO  appId 校验
        // 通过appid获取秘钥
        String secret = stringStringMap.get(appId);
        String token = request.getHeader(ApiSignConst.HEADER);
        // TODO  token 校验
        ApiSignContext.getSignUtil().checkParamMap(paramMap, secret);
    }
}
